package com.jlkf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.dao.UserMapper;
import com.jlkf.dao.UserMessageFormworkMapper;
import com.jlkf.dao.UserMessageMapper;
import com.jlkf.pojo.SugFeedback;
import com.jlkf.pojo.User;
import com.jlkf.pojo.UserMessage;
import com.jlkf.pojo.UserMessageFormwork;
import com.jlkf.service.UserMessageFormworkService;
import com.jlkf.service.util.AuroraPush;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.RegexUtil;
import com.platform.server.util.StringTools;
@Service
public class UserMessageFormworkServiceImpl implements UserMessageFormworkService{

	@Autowired
	private UserMessageFormworkMapper userMessageFormworkMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserMessageMapper userMessageMapper;
	
	@Override
	public Object getList(String formworkName, Integer pageNo, Integer pageSize) {
		
		Map<String,Object> map = new HashMap<>();
		if(RegexUtil.isNull(pageNo)){
			pageNo=1;
		}
		if(RegexUtil.isNull(pageSize)){
			pageSize=10;
		}
		if(!RegexUtil.isNull(formworkName)){
			map.put("formworkName", formworkName);
		}
		try {
			PageHelper.startPage(pageNo, pageSize);
			List<UserMessageFormwork> list=userMessageFormworkMapper.getList(map);
			PageInfo<UserMessageFormwork> info = new PageInfo(list);
			Integer totalPage= info.getPages();
			Integer totalRecord = (int) info.getTotal();
			return new ReturnData(list,totalPage,totalRecord,200,"feedback list ");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public Object addOrUpdate(String json) {
		UserMessageFormwork umf = StringTools.json2Obj(json, UserMessageFormwork.class);
		if(umf==null){
			return new ReturnData(201,"The parameter list is empty!");
		}
		try {
			if(umf.getUmfId()!=null){
				int num=userMessageFormworkMapper.updateByPrimaryKeySelective(umf);
				if(num==0){
					return new ReturnData(202,"fail");
				}
			}else{
				umf.setCreateTime(new Date());
				int num=userMessageFormworkMapper.insertSelective(umf);
				if(num==0){
					return new ReturnData(202,"fail");
				}
			}
			return new ReturnData(200,"success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public Object del(String ids) {
		try {
			String[] strlist=ids.split(",");
			for(String str:strlist){
				int num=userMessageFormworkMapper.deleteByPrimaryKey(Long.parseLong(str));
				if(num==0){
					return new ReturnData(201,"删除失败");
				}
			}
			return new ReturnData(200,"删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public Object sendMag(String ids,Long id) {
		Map<String,Object> map = new HashMap<>();
		map.put("ids", ids);
		try {
			UserMessageFormwork um=userMessageFormworkMapper.selectByPrimaryKey(id);
			List<User> list = userMapper.getNameAndTel(map);
			for(User u:list){
				UserMessage umm = new UserMessage();
				umm.setCreateTime(new Date());
				umm.setmContext(um.getmContext());
				umm.setmSummary(um.getmSummary());
				umm.setTelephone(u.getTelephone());
				umm.setName(u.getFirstName());
				userMessageMapper.insertSelective(umm);
			}
			return new ReturnData(200,"保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

}
